package com.danger.Activity.Audio;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.danger.Activity.R;
import com.danger.Model.bean.VideoBean;
import com.danger.MyApplication;
import com.danger.Presenter.ExtractAudioPresenter;
import com.danger.Presenter.ExtractAudioPresenterImpl;
import com.danger.Util.ImageUtil.BitmapBlur;
import com.danger.Util.FileCacheService;
import com.danger.View.ExtractAudioView;
import com.danger.Widget.XCRoundRectImageView;

import java.io.File;
import java.util.Map;

public class ExtractAudioActivity extends AppCompatActivity implements ExtractAudioView, View.OnClickListener {

    private Intent intent;
    private RelativeLayout layout;
    private ProgressDialog dialog;
    private Button extract_audio_content_button;
    private TextView extract_audio_has_content_textView;
    private Toolbar toolbar;
    private ExtractAudioPresenter presenter;
    private VideoBean videoBean;
    private XCRoundRectImageView video_thum_imageView;
    private String audio_path;
    private String type;      //音频类型
    private String rate;      //音频的采样率
    private String audio_name;
    private File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_extract_audio);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

        initView();
        initEvent();

    }

    private void initView() {
        intent = getIntent();
        if (intent != null) {
            videoBean = (VideoBean) intent.getSerializableExtra("videoBean");
            audio_path = videoBean.getDate();
            audio_name = videoBean.getTitle();
        }
        layout = (RelativeLayout) findViewById(R.id.extract_audio_relative);
        layout.setBackground(new BitmapDrawable(new BitmapBlur(this).blurBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.background))));
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        dialog = new ProgressDialog(this);
        dialog.setCanceledOnTouchOutside(false);
        extract_audio_content_button = (Button) findViewById(R.id.extract_audio_content_button);
        extract_audio_has_content_textView = (TextView) findViewById(R.id.extract_audio_has_content_textView);
        video_thum_imageView = (XCRoundRectImageView) findViewById(R.id.video_thum_imageView);
        presenter = new ExtractAudioPresenterImpl(this, this);
        presenter.setThumb(videoBean);
        FileCacheService service = new FileCacheService(this);
        Map<String, ?> map = service.getSharePreference("setting");
        type = map.get("audio_type").toString();
        rate = map.get("audio_rate").toString();
        file = new File(Environment.getExternalStorageDirectory() + "/vitamio/content/" + videoBean.getTitle() + ".wav");
        if (file.exists()) {
            extract_audio_has_content_textView.setVisibility(View.VISIBLE);
            extract_audio_content_button.setVisibility(View.GONE);
        }
    }

    private void initEvent() {
        extract_audio_content_button.setOnClickListener(this);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void showSuccess(String path, String name) {
        if (!file.exists() && path != null) {
            extract_audio_content_button.setVisibility(View.GONE);
            Toast.makeText(this, "文本获取成功,路径为:" + path + "  " + name, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, name, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void showDialog(String msg) {
        dialog.setMessage(msg);
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.extract_audio_content_button:
                presenter.getAudioContent(audio_path, audio_name, rate, type);
                break;
            default:
                break;
        }
    }

    @Override
    public ImageView getImageView() {
        return video_thum_imageView;
    }
}